package org.csu.mypetstore_ssm.controller;

import com.alibaba.fastjson2.JSON;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.csu.mypetstore_ssm.domain.Product;
import org.csu.mypetstore_ssm.persistence.ProductMapper;
import org.csu.mypetstore_ssm.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author RongXing 1360908370@qq.com
 * @version 2023/3/31 15:33
 * @ since JDK11
 */
@RestController
@RequestMapping("auto")
public class AutoCompleteController {

    @Autowired
    ProductMapper productMapper;

    @Autowired
    CatalogService catalogService;

//    private String result;

    @GetMapping("complete*")
    private List<Product> autoComplete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String keyword = request.getParameter("keyword");
        System.out.println(keyword);
        List<Product> products = catalogService.searchProductList(keyword);
//        List<Product> products = productMapper.searchProductList(keyword);
//        result = JSON.toJSONString(products);

//        response.setContentType("text/json");
//        PrintWriter out = response.getWriter();
//        out.println(result);
        return products;
    }
}
